<!--新手任务-->
<template>
  <div class="all bg2" @viewappear="viewappear">
    <div class="bg-refresh" v-if="datas"></div>

    <!--内容-->
    <scroller class="all" v-if="datas">

      <!--刷新-->
      <refresh class="refresh" @refresh="httpTaskList" :display="isRefresh ? 'show' : 'hide'">
        <loading-indicator class="load text2"></loading-indicator>
      </refresh>

      <div class="wrapper bg2">
        <LImage class="img-head" :src="Config.img(isNew ? imgHeadNmaeNew : imgHeadNmae)"></LImage>

        <template v-for="(item, index) in dailys">
          <div class="title aend" v-if="index == 0" v-bind:key="'title'+index">
            <text class="font36 text2">{{T('日常任务')}}</text>
            <div class="width10"></div>
            <text class="font26 text3">{{T('完成所有日常任务，奖励3VTN')}}</text>
          </div>
          <VTask :item="item" v-bind:key="index" @click="taskClick"></VTask>
        </template>
          
        <template v-for="(item, index) in datas">
          <div class="title" v-if="index == 0" v-bind:key="'title'+index">
            <text class="font36 text2">{{isNew ? T('新手任务') : T('成就任务')}}</text>
          </div>
          <VTask :item="item" v-bind:key="index" @click="taskClick"></VTask>
        </template>

        <div class="genduo-bg">
          <text class="font22 text3" v-if="isNew">{{T('完成新手任务解锁更多任务奖励')}}</text>
          <text class="font26 text3" v-else>{{T('没有更多任务了')}}</text>
        </div>
      </div>
    </scroller>
  </div>
</template>

<style src="@/_css/style.css" ></style>
<style scoped>
.img-head{
  background-color: #3232F3;
  width: 750px;
  height: 330px;
  border-bottom-left-radius: 24px;
  border-bottom-right-radius: 24px;
  overflow: hidden;
}

/*********************** 列表*/
.title {
  flex-direction: row;
  padding-left: 30px;
  padding-right: 30px;
  padding-top: 30px;
}
.list {
  flex: 1;
}
/*********************** 刷新*/
.bg-refresh {
  background-color: #3232F3;
  position: absolute;
  height: 400px;
  top: 0;
  left: 0;
  right: 0;
}
.refresh {
  width: 750;
  flex-direction: row;
  justify-content: center;
}
.load {
  height: 40px;
  width: 40px;
}
.genduo-bg {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 120px;
}
</style>

<script>

//组件
import VTask from '@/task/view/VTask.vue'; 

//功能
import jnavigator from "@/_js/navigator";
import jhttps from "@/_js/https";
import jconfig from "@/_js/config";
import jmodal from "@/_js/modal";
import jmath from "@/_js/math";
import jtime from "@/_js/time";
import jdebug from "@/_js/debug";
import jbroadcast from "@/_js/broadcast";

const globalEvent = weex.requireModule('globalEvent');
const clipboard = weex.requireModule("clipboard");
const weexFunction = weex.requireModule("WeexFunction");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T("任务"));

export default {
  
  components: {
    VTask,
  },


  data() {
    return {
      T: T,
      Config: jconfig,
      isRefresh: false, //turn 刷新

      imgHeadNmae:'',
      imgHeadNmaeNew:'',
      
      isNew:true, //true 新人任务
      datas: undefined, //普通任务
      dailys: undefined, //日常任务
      ops:{'1':{'opType':'open','url':'invite/Invite'},
           '2':{'opType':'openDialog','url':'mine/Signin'},
           '3':{'opType':'open','url':'home/ApiAdd'},
           '4':{'opType':'up'},
           '5':{'opType':'follw'},
           '6':{'opType':'shop'},
           '7':{'opType':'open','url':'wallet/WalletBuy', 'param':{'wallet':'consumeWallet'}, 'nav':{'navColor':'#0c0c0c'}},
           '8':{'opType':'follw'},
           '9':{'opType':'open','url':'invite/Invite'},
           '10':{'opType':'up'},
           '11':{'opType':'up'},
           '12':{'opType':'up'},
           '13':{'opType':'open','url':'mine/Vip'},
           '14':{'opType':'open','url':'server/Server'},
           '15':{'opType':'run'},
           '16':{'opType':'openDialog','url':'mine/Signin'},
           '17':{'opType':'up'},
      },
    };
  },

  created() {
    
    var isEnd = weex.config.language == 'en';
    this.imgHeadNmae = isEnd ? 'v2_lrw_laoyonghu_en.png' : 'v2_lrw_laoyonghu.png';
    this.imgHeadNmaeNew = isEnd ? 'v2_xrw_xinshou_en.png' : 'v2_xrw_xinshou.png';
    this.httpTaskList();
  },

  methods: {
    
    /*************************************************通信触发*/
    //完全显示
    viewappear() {
      this.httpTaskList();
    },

    /********************************** HTTP*/
    //http任务列表
    httpTaskList() {
      if (this.isRefresh) return;
      this.isRefresh = true;

      var param = {};
      jhttps.post("activity/getTaskList",param,true,(data) => {
        this.isRefresh = false;
        if (data.status == 200) {
          data = data.data;
          this.isNew = data.isNew;

          if (data.isNew) {

            weexFunction.setTitle(T("新手任务"));
            this.datas = data.taskList;
          } else {

            weexFunction.setTitle(T("老用户任务"));
            var dailys = [];
            var datas = [];
            for (let index = 0; index < data.taskList.length; index++) {
              var task = data.taskList[index];
              task.type == 'ACHIEVEMENT' ? datas.push(task) : dailys.push(task);
            }
            this.dailys = dailys;
            this.datas = datas;
          }
        } else {
          jmodal.toast(data.msg);
        }
      });
    },

    //http领奖
    httpGetReward(item) {

      jmodal.showLoad();
      var param = {'taskId':Number(item.taskId)};
      jhttps.postX("activity/receiveRewards",param,true,(data) => {
        jmodal.hideLoad();
        if (data.status == 200) {
          item.status = 'REWARDED';
          jmodal.toast(T('领取成功'));
          this.checkAll();
        } else {
          jmodal.toast(data.msg);
        }
      });
    },

    /********************* 其他*/
    //检查完成所有任务
    checkAll() {
      for (let index = 0; index < this.datas.length; index++) {
        var data = this.datas[index];
        if (data.status != 'REWARDED') {
          return;
        }
      }
      var param = {'isNew':this.isNew};
      jnavigator.openDialog('task/TaskOk', param);
    },

    /********************* 点击*/
    //任务操作点击
    taskClick(item) {

      //领取奖励
      if (item.status == 'COMPLETED') {
        this.httpGetReward(item);
        return;
      }

      //其他操作
      var op = this.ops[item.taskId];

      if (op.opType == 'open') {
        jnavigator.open('', op.url, op.param, op.nav);
        return;
      }

      if (op.opType == 'openDialog') {
        jnavigator.openDialog(op.url);
        return;
      }

      if (op.opType == 'up') {
        weexFunction.setShowTabBar(1);
        weexFunction.postNotificationName('StrategyShow', {'index':0});
        jnavigator.back(3);
        return;
      }

      if (op.opType == 'follw') {
        weexFunction.setShowTabBar(1);
        weexFunction.postNotificationName('StrategyShow', {'index':2});
        jnavigator.back(3);
        return;
      }

      if (op.opType == 'shop') {
        weexFunction.setShowTabBar(1);
        weexFunction.postNotificationName('StrategyShow', {'index':1});
        jnavigator.back(3);
        return;
      }

      if (op.opType == 'run') {
        weexFunction.setShowTabBar(1);
        weexFunction.postNotificationName('StrategyShow', {'index':0});
        jbroadcast.send('UpShow', {'index':1});
        jnavigator.back(3);
        return;
      }
    }
  },
};
</script>